/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <QtGui>

#include "captchaWizard.h"

captchaWizard::captchaWizard(QPointer<xmlUrlContainer> urlC, QUrl url, QString _folder, QWidget *parent)
	: QWizard(parent)
{
        folder = _folder;

	setPage(Page_Captcha, new captchaPage(urlC, url, this));

	myLinksPage =  new linksPage(urlC, this);
	setPage(Page_Links, myLinksPage);

        myFolderPage = new folderPage(this);
        setPage(Page_Folder, myFolderPage);

	//page(Page_Links)->table

	setStartId(Page_Captcha);

	setOption(HaveHelpButton, false);

	setWizardStyle(ModernStyle);

    /*QList<QWizard::WizardButton> layout;
    layout << QWizard::CancelButton << QWizard::FinishButton;
    setButtonLayout(layout);*/

	setWindowTitle(tr("Captcha Wizard"));
}

bool captchaWizard::validateCurrentPage()
{
	if(currentId() == Page_Links)
	{
		urlList = QStringList();

		for(int i=0; i<myLinksPage->model()->rowCount(); i++)
		{
		    if(myLinksPage->model()->data(myLinksPage->model()->index(i,0), Qt::CheckStateRole) == Qt::Checked)
			urlList.append(myLinksPage->model()->item(i,1)->text());
		}
        } else if(currentId() == Page_Folder)
        {
            folder = myFolderPage->folder()->text();
        }

	return true;
}

captchaPage::captchaPage(QPointer<xmlUrlContainer> urlContainer, QUrl url, QPointer<captchaWizard> parent)
	: QWizardPage(parent)
{
	Q_UNUSED(url);

	setTitle(tr("Captcha"));
	urlC = urlContainer;
	cWizard = parent;

	connect(urlC, SIGNAL(captcha(const QString &)), this, SLOT(slot_urlC_captcha(const QString &)));
	urlC->start();

	topLabel = new QLabel(tr("Please input captcha."));
        topLabel->setWordWrap(false);

	captchaLabel = new QLabel(tr(""));
	registerField("captchaUrl", captchaLabel);

	lineEditCaptcha = new QLineEdit("");
	registerField("lineEditCaptcha*", lineEditCaptcha);

	QPointer<QGridLayout> layout = new QGridLayout;

	layout->addWidget(topLabel, 0, 0);
	layout->addWidget(captchaLabel, 1, 0);
	layout->addWidget(lineEditCaptcha, 2, 0);

	setLayout(layout);
}

void captchaPage::slot_urlC_captcha(const QString &captcha)
{
	if(captcha == "-1")
	{
		setField("captcha", false);
		wizard()->next();
	} else
	{
		if(!cWizard->captcha.isEmpty()) QFile::remove(cWizard->captcha);

		cWizard->captcha = captcha;
		setPixmap(QWizard::WatermarkPixmap, captcha);
	}
}

int captchaPage::nextId() const
{
    return captchaWizard::Page_Links;
}

linksPage::linksPage(QPointer<xmlUrlContainer> urlContainer, QPointer<captchaWizard> parent)
	: QWizardPage(parent)
{
	urlC = urlContainer;
	cWizard = parent;

	setTitle(tr("Links"));

	connect(urlC, SIGNAL(finished(const QStringList &, const bool &)), this, SLOT(slot_urlC_finished(const QStringList &, const bool &)));
	connect(urlC, SIGNAL(captcha(const QString &)), this, SLOT(slot_urlC_captcha(const QString &)));
	connect(urlC, SIGNAL(progressChanged(const int &)), this, SLOT(slot_urlC_progressChanged(const int &)));

	topLabel = new QLabel(tr("Files Found:"));
        topLabel->setWordWrap(false);

        checkPushButton = new QPushButton(tr("Uncheck All"));
        connect(checkPushButton, SIGNAL(clicked()), this, SLOT(slot_checkPushButton_clicked()));

	treeView = new QTreeView;
	treeView->setMinimumSize(600, 400);

        modelFiles = new QStandardItemModel(0, 2);
        modelFiles->setHeaderData(0, Qt::Horizontal, tr("Download"));
	modelFiles->setHeaderData(1, Qt::Horizontal, tr("Filename"));
        //modelFiles->setHeaderData(2, Qt::Horizontal, tr("Size"));

	treeView->setModel(modelFiles);
	treeView->setAlternatingRowColors(true);
	treeView->setUniformRowHeights(true);
	treeView->setIconSize(QSize(24, 24));

	progressBar = new QProgressBar;
	progressBar->setAlignment(Qt::AlignCenter);

	QPointer<QGridLayout> layout = new QGridLayout;
	layout->addWidget(topLabel, 0, 0);
	layout->addWidget(progressBar, 0, 1);

	layout->addWidget(treeView, 1, 0, 1, 2);
        layout->addWidget(checkPushButton, 2, 0);

	setLayout(layout);
}

void linksPage::initializePage()
{
	//if(wizard()->field("captcha").toBool())
	cWizard->captchaInput = field("lineEditCaptcha").toString();
	urlC->submitCaptcha(cWizard->captchaInput);

	setField("lineEditCaptcha", "");
}

void linksPage::slot_urlC_finished(const QStringList &urlList, const bool &error)
{
	if(!error)
	{
		int tmpInt = cWizard->captcha.lastIndexOf(".");
		QString newName = cWizard->captcha;
		newName.insert(tmpInt, "_" + cWizard->captchaInput);

		QFile::rename(cWizard->captcha, newName);
		cWizard->captcha = "";

		for(int i=0; i<urlList.count(); i++)
		{
			QList<QStandardItem *> listItem;

			//checked
			QStandardItem *item = new QStandardItem("");
                        item->setCheckable(true);
                        item->setEditable(false);
                        item->setCheckState(Qt::Checked);
			listItem.append(item);

			//filename
			QStandardItem *itemFilename = new QStandardItem(urlList.at(i));
                        itemFilename->setEditable(false);
			listItem.append(itemFilename);

			//size
                        /*QStandardItem *itemText = new QStandardItem("100 MB");
                        itemText->setEditable(false);
                        listItem.append(itemText);*/

			modelFiles->appendRow(listItem);
                        //modelFiles->setData(modelFiles->index(modelFiles->rowCount()-1,0), Qt::Checked, Qt::CheckStateRole);
		}
	}
}

void linksPage::slot_urlC_progressChanged(const int &progress)
{
	progressBar->setValue(progress);
}

void linksPage::slot_urlC_captcha(const QString &captcha)
{
	Q_UNUSED(captcha);
	wizard()->back();
}

int linksPage::nextId() const
{
    return captchaWizard::Page_Folder;
}

void linksPage::slot_checkPushButton_clicked()
{
    Qt::CheckState checkState = Qt::Checked;

    if(checkPushButton->text() == tr("Check All"))
    {
        checkState = Qt::Checked;
        checkPushButton->setText(tr("Uncheck All"));
    } else
    {
        checkState = Qt::Unchecked;
        checkPushButton->setText(tr("Check All"));
    }

    for(int i=0; i<modelFiles->rowCount(); ++i)
    {
        modelFiles->item(i,0)->setCheckState(checkState);
    }
}

folderPage::folderPage(QPointer<captchaWizard> parent)
        : QWizardPage(parent)
{
        cWizard = parent;

        setTitle(tr("Folder"));

        topLabel = new QLabel(tr("Select Folder:"));
        topLabel->setWordWrap(false);

        folderLineEdit = new QLineEdit(cWizard->folder);

        QPointer<QGridLayout> layout = new QGridLayout;
        layout->addWidget(topLabel, 0, 0);
        layout->addWidget(folderLineEdit, 0, 1);

        setLayout(layout);
}

int folderPage::nextId() const
{
    return -1;
}
